package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.AlipayMchntInfoMapper;
import cc.rengu.igas.bomp.common.entity.AlipayActivityMchntInfo;
import cc.rengu.igas.bomp.common.entity.AlipayMchntInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * Created by 王成 on 2020/3/2
 */
public class AlipayMchntInfoMapperImpl implements AlipayMchntInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertAlipayMchntInfo(AlipayMchntInfo alipayMchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertBompAlipayMchntInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, alipayMchntInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记支付宝商户基本信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", alipayMchntInfo.getInstId(), alipayMchntInfo.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateAlipayMchntInfoById(AlipayMchntInfo alipayMchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBompAlipayMchntInfoById");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, alipayMchntInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新支付宝商户基本信息失败,instId:<{}>,mchntNo:<{}>,id:<{}>,iReturnCode:<{}>", alipayMchntInfo.getInstId(), alipayMchntInfo.getMchntNo(), alipayMchntInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }


    @Override
    public int updateAlipayMchntInfoNoTransactionById(AlipayMchntInfo alipayMchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBompAlipayMchntInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, alipayMchntInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新支付宝商户基本信息失败,instId:<{}>,mchntNo:<{}>,id:<{}>,iReturnCode:<{}>", alipayMchntInfo.getInstId(), alipayMchntInfo.getMchntNo(), alipayMchntInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }


    @Override
    public AlipayMchntInfo selectAlipayMchntInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompAlipayMchntInfoById");
        AlipayMchntInfo alipayMchntInfo = new AlipayMchntInfo();
        alipayMchntInfo.setInstId(instId);
        alipayMchntInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, alipayMchntInfo, alipayMchntInfo);
        if (0 != iReturnCode) {
            rglog.error("获取支付宝商户基本信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return alipayMchntInfo;
    }

    @Override
    public AlipayMchntInfo selectAlipayMchntInfoByPrimaryKey(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompAlipayMchntInfoByPrimaryKey");
        AlipayMchntInfo alipayMchntInfo = new AlipayMchntInfo();
        alipayMchntInfo.setInstId(instId);
        alipayMchntInfo.setMchntNo(mchntNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, alipayMchntInfo, alipayMchntInfo);
        if (0 != iReturnCode) {
            rglog.error("获取支付宝商户基本信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, mchntNo, iReturnCode);
            return null;
        }
        return alipayMchntInfo;
    }

    @Override
    public AlipayMchntInfo selectAlipayMchntInfoByAlipayMchntNo(String instId, String alipayMchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompAlipayMchntInfoByAlipayMchntNo");
        AlipayMchntInfo alipayMchntInfo = new AlipayMchntInfo();
        alipayMchntInfo.setInstId(instId);
        alipayMchntInfo.setAlipayMchntNo(alipayMchntNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, alipayMchntInfo, alipayMchntInfo);
        if (0 != iReturnCode) {
            rglog.error("获取支付宝商户基本信息失败,instId:<{}>,alipayMchntNo:<{}>,iReturnCode:<{}>", instId, alipayMchntNo, iReturnCode);
            return null;
        }
        return alipayMchntInfo;
    }

    @Override
    public PageResult selectAlipayActivityMchntInfoByInstid(String instId, String mchntNo, String mchntName, String signInstId, String activityType, String activityApplyResult,
                                                            PageInfo pageInfo, String alipayActivityMcc, String orgCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectAlipayActivityMchntInfoByInstid");
        AlipayActivityMchntInfo alipayActivityMchntInfo = new AlipayActivityMchntInfo();
        alipayActivityMchntInfo.setInstId(instId);
        if (null != mchntNo && !mchntNo.isEmpty()) {
            alipayActivityMchntInfo.setMchntNo("%" + mchntNo + "%");
        }
        if (null != mchntName && !mchntName.isEmpty()) {
            alipayActivityMchntInfo.setMchntName("%" + mchntName + "%");
        }
        if (null != signInstId && !signInstId.isEmpty()) {
            alipayActivityMchntInfo.setSignInstId(signInstId);
        }
        if (null != activityType && !activityType.isEmpty()) {
            alipayActivityMchntInfo.setActivityType(activityType);
        }
        if (null != activityApplyResult && !activityApplyResult.isEmpty()) {
            alipayActivityMchntInfo.setActivityApplyResult(activityApplyResult);
        }
        if (null != alipayActivityMcc && !alipayActivityMcc.isEmpty()) {
            alipayActivityMchntInfo.setIndustryType(alipayActivityMcc);
        }
        alipayActivityMchntInfo.setArtifName(orgCode);
        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, alipayActivityMchntInfo, AlipayActivityMchntInfo.class, pageInfo);
        if (null == pageResult) {
            rglog.error("查询支付宝特殊费率商户基本信息失败,instId:<{}>,mchntNo:<{}>,mchntName:<{}>,signInstId:<{}>,activityType:<{}>,activityApplyResult:<{}>", instId, mchntNo, mchntName, signInstId, activityType, activityApplyResult);
            return null;
        }
        return pageResult;
    }
}
